package q2d.client;

import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.print.DocFlavor.STRING;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;

import q2d.common.models.units.Player;

public class StartWindow extends JFrame implements MouseListener{
	private Game game;
	private JLabel info1;
	private JLabel info2;
	private JLabel info3;
	private JLabel info4;
	private JLabel info5;
	private JLabel info6;
	private JTextField IPInsert1;
	private JTextField IPInsert2;
	private JTextField IPInsert3;
	private JTextField IPInsert4;
	private JTextField SerwerInsert;
	private JTextField ClientInsert;
	private JTextField PlayerName;
	
	private ButtonGroup teams;
	private JRadioButton BlueTeam;
	private JRadioButton RedTeam;
	private JButton StartButton;
	private Container cp;
	private JPanel myPanel;
	private InetAddress serverIP;
	private JCheckBox soundCheck;
	private ImageIcon img;
	public StartWindow(){
		super("Quake2D");
		myPanel = new JPanel();
		setBounds(430,200, 320, 360);
		setResizable(false);
		IPInsert1 = new JTextField(3);
		IPInsert1.setText("192");
		IPInsert2 = new JTextField(3);
		IPInsert2.setText("168");
		IPInsert3 = new JTextField(3);
		IPInsert3.setText("1");
		IPInsert4 = new JTextField(3);
		IPInsert4.setText("5");
		info1 = new JLabel("    IP Serwera");

		myPanel.add(info1);
		myPanel.add(IPInsert1);
		myPanel.add(IPInsert2);
		myPanel.add(IPInsert3);
		myPanel.add(IPInsert4);

		info2 = new JLabel("Port Serwera");
		SerwerInsert = new JTextField(16);
		SerwerInsert.setText("7777");
		myPanel.add(info2);
		myPanel.add(SerwerInsert);

		info3 = new JLabel("   Port Klienta");
		ClientInsert = new JTextField(16);
		ClientInsert.setText("7778");
		myPanel.add(info3);
		myPanel.add(ClientInsert);
		info4 = new JLabel("Nazwa gracza(max 10 znakow):");
		
		PlayerName = new JTextField(7);
		PlayerName.setText("NickName");
		myPanel.add(info4);
		myPanel.add(PlayerName);
		
		info5 = new JLabel("     Wybierz druzyne");
		
		BlueTeam = new JRadioButton("Niebieska",true);
		RedTeam = new JRadioButton("Czerwona");
		teams = new ButtonGroup();
		teams.add(BlueTeam);
		teams.add(RedTeam);
		myPanel.add(info5);
		myPanel.add(BlueTeam);
		myPanel.add(RedTeam);
		soundCheck = new JCheckBox("Wlacz/Wylacz dzwiek",true);
		myPanel.add(soundCheck);

		
		img = new ImageIcon("assets/1280x800/18pegi.png ");
		JLabel l = new JLabel(img);
		myPanel.add(l);
		
		StartButton = new JButton("Start Game");
		myPanel.add(StartButton);
		add(myPanel);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		StartButton.addMouseListener(this);
		setVisible(true);
	}
	public static void main(String[] args){
		StartWindow s = new StartWindow();
		
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		int ServerPortNumber,ClientPortNumber;
		boolean sound;
		byte b1,b2,b3,b4;
		String PlayerS = "";
		int team=0;
		
		if(e.getSource() == StartButton ){
			if(IPInsert1.getText().equals("")||IPInsert2.getText().equals("") || IPInsert3.getText().equals("")|| IPInsert4.getText().equals("") ) JOptionPane.showMessageDialog(this,"Uzupelnij pole Serwer IP ");
			
			else if(SerwerInsert.getText().equals("") || ClientInsert.getText().equals("")) JOptionPane.showMessageDialog(this, "Uzupelnij numery portow");
			else if(PlayerName.getText().equals("")) JOptionPane.showMessageDialog(this, "Wpisz nazwe gracza");
			else if(PlayerName.getText().length()>10) JOptionPane.showMessageDialog(this, "Nazwa gracza moze miec tylko 10 znakow");
			else{
					PlayerS = PlayerName.getText();
					b1= (byte) ((int)Integer.valueOf(IPInsert1.getText().trim()));
					b2= (byte) ((int)Integer.valueOf(IPInsert2.getText().trim()));
					b3= (byte) ((int)Integer.valueOf(IPInsert3.getText().trim()));
					b4= (byte) ((int)Integer.valueOf(IPInsert4.getText().trim()));
						try {
							serverIP = InetAddress.getByAddress(new byte[]{(byte)b1,(byte)b2,(byte)b3,(byte)b4});
						} catch (UnknownHostException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
						if(BlueTeam.isSelected())team = Player.BLUE_TEAM;
						else if(RedTeam.isSelected()) team = Player.RED_TEAM;
						ServerPortNumber = Integer.valueOf(SerwerInsert.getText().trim());
						ClientPortNumber = Integer.valueOf(ClientInsert.getText().trim());
						
						sound = soundCheck.isSelected();
						game = new Game(ServerPortNumber,ClientPortNumber,serverIP,PlayerS,team,sound);
						Thread t = new Thread( game);
						t.start();
			}	

			
		}
}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
		
	}


}
